package com.btpj.hospital_server.controller;

import cn.hutool.core.util.StrUtil;
import com.btpj.hospital_server.dto.CommonPage;
import com.btpj.hospital_server.dto.CommonResult;
import com.btpj.hospital_server.dto.param.HospitalInfoParam;
import com.btpj.hospital_server.entity.HospitalInfo;
import com.btpj.hospital_server.entity.HospitalSpecial;
import com.btpj.hospital_server.service.HospitalInfoService;
import com.btpj.hospital_server.service.HospitalOutpatientService;
import com.btpj.hospital_server.service.HospitalSpecialService;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 医院信息Controller
 *
 * @author BTPJ  2020/8/24
 */
@Api(tags = "医院信息接口")
@RestController
@RequestMapping("/hospital")
public class HospitalInfoController {

    @Resource
    HospitalInfoService hospitalInfoService;

    @Resource
    HospitalSpecialService hospitalSpecialService;

    @Resource
    HospitalOutpatientService hospitalOutpatientService;

    @ApiOperation("查询医院信息列表")
    @ApiImplicitParam(name = "name", value = "医院名称(模糊匹配)", dataType = "String", paramType = "query")
    @GetMapping("/list")
    public CommonResult<List<HospitalInfo>> getHospitalInfoList(@RequestParam(required = false) String name) {
        return CommonResult.success(hospitalInfoService.getList(name));
    }

    @ApiOperation("分页查询医院信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "医院名称(模糊匹配)", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageNo", value = "第几页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "int", paramType = "query")
    })
    @GetMapping("/listPage")
    public CommonResult<CommonPage<HospitalInfo>> getHospitalInfoPageList(@RequestParam(required = false) String name,
                                                                          @RequestParam Integer pageNo,
                                                                          @RequestParam Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        CommonPage<HospitalInfo> commonPage = CommonPage.listPage(hospitalInfoService.getList(name));
        return CommonResult.success(commonPage);
    }

    @ApiOperation("添加医院信息")
    @ApiImplicitParam(name = "name", value = "医院名称", required = true, paramType = "query", dataType = "String")
    @PostMapping("/add")
    public CommonResult<Void> addHospitalInfo(HospitalInfoParam param) {
        String name = param.getName();
        if (name == null || StrUtil.isEmpty(name.trim())) {
            return CommonResult.error("医院名称不可为空");
        }

        if (hospitalInfoService.existHospital(name)) {
            return CommonResult.error("已存在此名称的医院");
        }

        if (hospitalInfoService.addHospitalInfo(param)) {
            return CommonResult.success("添加医院信息成功");
        } else {
            return CommonResult.error("服务器错误，添加医院信息失败");
        }
    }

    @ApiOperation("更新医院信息")
    @ApiImplicitParam(name = "id", value = "医院ID", required = true, paramType = "path", dataType = "Long")
    @PutMapping("/update/{id}")
    public CommonResult<Void> updateHospitalInfo(@PathVariable Long id, HospitalInfoParam param) {
        if (!hospitalInfoService.existHospital(id)) {
            return CommonResult.error("不存在此编号的医院");
        }

        String name = param.getName();
        if (name != null) {
            if (StrUtil.isEmpty(name.trim())) {
                return CommonResult.error("医院名称不可为空");
            } else {
                if (hospitalInfoService.existHospital(name)) {
                    return CommonResult.error("已存在此名称的医院");
                }
            }
        }
        if (hospitalInfoService.updateHospitalInfo(id, param)) {
            return CommonResult.success("更新医院信息成功");
        } else {
            return CommonResult.error("服务器错误，更新医院信息失败");
        }
    }

    @ApiOperation("获取医院信息详情")
    @ApiImplicitParam(name = "id", value = "医院ID", required = true, paramType = "path", dataType = "Long")
    @GetMapping("/detail/{id}")
    public CommonResult<HospitalInfo> getHospitalInfo(@PathVariable Long id) {
        if (!hospitalInfoService.existHospital(id)) {
            return CommonResult.error("不存在此编号的医院");
        }

        HospitalInfo hospitalInfo = hospitalInfoService.getHospitalInfo(id);
        if (hospitalInfo != null) {
            return CommonResult.success(hospitalInfo);
        }
        return CommonResult.error("服务器错误，更新医院信息失败");
    }

    @ApiOperation("删除医院信息")
    @ApiImplicitParam(name = "id", value = "医院ID", required = true, paramType = "path", dataType = "Long")
    @DeleteMapping("/delete/{id}")
    public CommonResult<Void> deleteHospitalInfo(@PathVariable Long id) {
        if (!hospitalInfoService.existHospital(id)) {
            return CommonResult.error("不存在此编号的医院");
        }

        if (hospitalInfoService.deleteHospitalInfo(id)) {
            return CommonResult.success("删除医院信息成功");
        }

        return CommonResult.error("服务器错误，删除医院信息失败");
    }

    @ApiOperation("向医院中添加专科信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalId", value = "医院ID", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "specialId", value = "专科ID", required = true, paramType = "query", dataType = "Long")
    })
    @PostMapping("/specialRelation/add")
    public CommonResult<Void> addSpecialRelation(@RequestParam Long hospitalId, @RequestParam Long specialId) {
        if (!hospitalInfoService.existHospital(hospitalId)) {
            return CommonResult.error("不存在该医院编号");
        }
        if (!hospitalSpecialService.existSpecial(specialId)) {
            return CommonResult.error("不存在该专科编号");
        }
        if (hospitalInfoService.existSpecialRelation(hospitalId, specialId)) {
            return CommonResult.error("当前医院已存在该专科");
        }
        if (hospitalInfoService.addSpecialRelation(hospitalId, specialId)) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.error("服务器错误，添加专科信息失败");
    }

    @ApiOperation("在指定医院中移除专科信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalId", value = "医院ID", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "specialId", value = "专科ID", required = true, paramType = "query", dataType = "Long")
    })
    @DeleteMapping("/specialRelation/delete")
    public CommonResult<Void> removeSpecialRelation(@RequestParam Long hospitalId, @RequestParam Long specialId) {
        if (!hospitalInfoService.existHospital(hospitalId)) {
            return CommonResult.error("不存在该医院编号");
        }
        if (!hospitalSpecialService.existSpecial(specialId)) {
            return CommonResult.error("不存在该专科编号");
        }
        if (!hospitalInfoService.existSpecialRelation(hospitalId, specialId)) {
            return CommonResult.error("当前医院不存在该专科");
        }
        if (hospitalInfoService.deleteSpecialRelation(hospitalId, specialId)) {
            return CommonResult.success("移除成功");
        }
        return CommonResult.error("服务器错误，删除专科信息失败");
    }

    @ApiOperation("向医院中添加门诊")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalId", value = "医院ID", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "outpatientId", value = "门诊ID", required = true, paramType = "query", dataType = "Long")
    })
    @PostMapping("/outpatientRelation/add")
    public CommonResult<Void> addOutpatientRelation(@RequestParam Long hospitalId, @RequestParam Long outpatientId) {
        if (!hospitalInfoService.existHospital(hospitalId)) {
            return CommonResult.error("不存在该医院编号");
        }
        if (!hospitalOutpatientService.existOutpatient(outpatientId)) {
            return CommonResult.error("不存在该门诊编号");
        }
        if (hospitalInfoService.existOutpatientRelation(hospitalId, outpatientId)) {
            return CommonResult.error("当前医院已存在该门诊");
        }
        if (hospitalInfoService.addOutpatientRelation(hospitalId, outpatientId)) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.error("服务器错误，添加门诊信息失败");
    }

    @ApiOperation("在指定医院中移除门诊信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalId", value = "医院ID", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "outpatientId", value = "门诊ID", required = true, paramType = "query", dataType = "Long")
    })
    @DeleteMapping("/outpatientRelation/delete")
    public CommonResult<Void> removeOutpatientRelation(@RequestParam Long hospitalId, @RequestParam Long outpatientId) {
        if (!hospitalInfoService.existHospital(hospitalId)) {
            return CommonResult.error("不存在该医院编号");
        }
        if (!hospitalOutpatientService.existOutpatient(outpatientId)) {
            return CommonResult.error("不存在该门诊编号");
        }
        if (!hospitalInfoService.existOutpatientRelation(hospitalId, outpatientId)) {
            return CommonResult.error("当前医院不存在该门诊");
        }
        if (hospitalInfoService.deleteOutpatientRelation(hospitalId, outpatientId)) {
            return CommonResult.success("移除成功");
        }
        return CommonResult.error("服务器错误，删除门诊信息失败");
    }

    @ApiOperation("查询医院所有专科列表")
    @ApiImplicitParam(name = "hospitalId", value = "医院Id", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/special/list/{hospitalId}")
    public CommonResult<List<HospitalSpecial>> getHospitalSpecialList(@PathVariable Long hospitalId) {
        return CommonResult.success(hospitalInfoService.getSpecialList(hospitalId));
    }

    @ApiOperation("分页查询医院所有专科列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalId", value = "医院Id", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "pageNo", value = "第几页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "int", paramType = "query")
    })
    @GetMapping("/special/listPage/{hospitalId}")
    public CommonResult<CommonPage<HospitalSpecial>> getHospitalSpecialPageList(@PathVariable Long hospitalId,
                                                                                @RequestParam Integer pageNo,
                                                                                @RequestParam Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        CommonPage<HospitalSpecial> commonPage = CommonPage.listPage(hospitalInfoService.getSpecialList(hospitalId));
        return CommonResult.success(commonPage);
    }
}
